﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestPlatform.Core;
using InDB.Client;
using TestPlatform.Performer.InDB.Entities;

namespace TestPlatform.Performer.InDB
{
    public class InDBHelper : IDatabaseHelper
    {
        private static ISessionFactory factory = null;

        public static ISession OpenSession()
        {
            if (factory == null)
            {
                factory = SessionFactoryFactory.CreateSessionFactory();
            }

            return factory.OpenSession();
        }

        public List<Guid> SeedDatabase(int count)
        {
            List<Guid> ids = new List<Guid>();

            using (ISession s = OpenSession())
            {
                for (int i = 0; i < count; i++)
                {
                    Guid id = Guid.NewGuid();
                    s.Save(id, new User { Id = id, Login = id.ToString(), Name = id.ToString() });
                    ids.Add(id);
                }
            }

            return ids;
        }

        public void ClearDatabase()
        {
            using (ISession session = OpenSession())
            {
                session.DropCollection(typeof(User));
            }
        }
    }
}
